﻿import flash.geom.Rectangle;
import kliment.display.Sprite;
/**
 * ...
 * @author	Kliment
 * @version	1.0.2
 */
class kliment.display.MC9Slice extends Sprite {
	private var _width:Number;
	private var _height:Number;
	
	private var _min_width:Number = 25;
	private var _min_height:Number = 25;
	
	private var _tl_mc:MovieClip;
	private var _tr_mc:MovieClip;
	private var _bl_mc:MovieClip;
	private var _br_mc:MovieClip;
	
	private var _ct_mc:MovieClip;
	private var _cl_mc:MovieClip;
	private var _cb_mc:MovieClip;
	private var _cr_mc:MovieClip;
	private var _c_mc:MovieClip;
	
	/**
	 * 
	 * @param	target MovieClip
	 * @param	options Object {tl:MovieClip, tr:MovieClip, bl:MovieClip, br:MovieClip, ct:MovieClip, cl:MovieClip, cb:MovieClip, cr:MovieClip, min_width:Number, min_height:Number, width:Number, height:Number}
	 */
	public function MC9Slice(target:MovieClip, options:Object) {
		_target_mc = target;
		
		_tl_mc = options.tl;
		_tr_mc = options.tr;
		_bl_mc = options.bl;
		_br_mc = options.br;
		_ct_mc = options.ct;
		_cl_mc = options.cl;
		_cb_mc = options.cb;
		_cr_mc = options.cr;
		_c_mc = options.c;
		
		if (!isNaN(options.min_width)) _min_width = options.min_width;
		if (!isNaN(options.min_height)) _min_height = options.min_height;
		if (!isNaN(options.width)) width = options.width;
		if (!isNaN(options.height)) height = options.height;
		
		_init();
	}
	
	private function _init():Void {
		_tl_mc._x = 0;
		_tl_mc._y = 0;
		_ct_mc._y = 0;
		
		_tr_mc._x = _min_width;
		_tr_mc._y = 0;
		
		_br_mc._x = _min_width;
		_br_mc._y = _min_height;
		
		_bl_mc._x = 0;
		_bl_mc._y = _min_height;
		
		_setHorisontal();
		_setVaertical();
	}
	
	public function get width():Number { return _width }
	public function set width(value:Number):Void {
		value = Math.max(value, _min_width);
		_tr_mc._x = 
		_br_mc._x = value;
		_setHorisontal();
		_width = value;
	}
	
	public function get height():Number { return _height }
	public function set height(value:Number):Void {
		value = Math.max(value, _min_height);
		_bl_mc._y = 
		_br_mc._y = value;
		_setVaertical();
		_height = value;
	}
	
	private function _setHorisontal():Void {
		var tlBounds:Object = _tl_mc.getBounds(_tl_mc._parent);
		var blBounds:Object = _bl_mc.getBounds(_bl_mc._parent);
		
		var trBounds:Object = _tr_mc.getBounds(_tr_mc._parent);
		var brBounds:Object = _br_mc.getBounds(_br_mc._parent);
		
		_cr_mc._x = _tr_mc._x;
		
		_ct_mc._x = tlBounds.xMax;
		_ct_mc._width = trBounds.xMin - tlBounds.xMax;
		
		_cb_mc._x = blBounds.xMax;
		_cb_mc._width = brBounds.xMin - blBounds.xMax;
		
		_c_mc._x = _ct_mc._x;
		_c_mc._width = _ct_mc._width;
	}
	
	private function _setVaertical():Void {
		var tlBounds:Object = _tl_mc.getBounds(_tl_mc._parent);
		var blBounds:Object = _bl_mc.getBounds(_bl_mc._parent);
		
		var trBounds:Object = _tr_mc.getBounds(_tr_mc._parent);
		var brBounds:Object = _br_mc.getBounds(_br_mc._parent);
		
		_cb_mc._y = _bl_mc._y;
		
		_cl_mc._y = tlBounds.yMax;
		_cl_mc._height = brBounds.yMin - tlBounds.yMax;
		
		_cr_mc._y = trBounds.yMax;
		_cr_mc._height = brBounds.yMin - trBounds.yMax;
		
		_c_mc._y = _cl_mc._y;
		_c_mc._height = _cl_mc._height;
	}
	
}